package com.eleMall.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eleMall.entity.Market;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;


/**
 * @author XiMen
 * @version 1.0
 * @description
 * @date 2024/3/20 22:59
 */
public class getMarkets {
    public static List<Market> getMarkets() throws Exception{
        URL url = new URL("http://www.xinfadi.com.cn/getCat.html?prodCatid=1190");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuffer content = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        connection.disconnect();
        //String转json
        JSONObject jsonObject = JSONObject.parseObject(content.toString());
        String list = jsonObject.getString("list");

        List<Market> markets = JSON.parseArray(list, Market.class);

        return markets;
    }

    public static void main(String[] args) throws Exception {
        getMarkets();
    }
}
